package main
import (
	"fmt"
	"net"
	"strings"
)

func main(){
	
	/*监听*/
	lt, err := net.Listen("tcp", "127.0.0.1:8000")
	if err != nil{
		fmt.Println("Listen err : ", err)
		return
	}
	fmt.Println("Link TCP 127.0.0.1:8000 OK")
	defer lt.Close()
	
	/*阻塞*/
	for{
		conn, err1 := lt.Accept()
		if err1 != nil{
			fmt.Println("Accept err ：", err1)
			continue
		}
		/*接收到客户端连接*/
		addr := conn.RemoteAddr()
		fmt.Printf("%s 已连接上线！\n",addr)
		go Kehuduan(conn, addr)
	}
}

func Kehuduan(conn net.Conn, addr net.Addr){
	
		defer conn.Close()
		for{
			/*输出*/
			buf := make([]byte, 1024)
			
			num, err2 := conn.Read(buf)
			if err2 != nil{
				fmt.Println(addr, "Read err ：", err2)
				return
			}
			if strings.ToUpper(string(buf[:num])) == "EXIT"{
				fmt.Println(addr, "用户已经断开连接！")
				return
			}
			fmt.Printf("%s TCP客户端发来数据：%s\n", addr, buf[:num])
		}
}